package com.lcredu.lcrliving.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcredu.common.util.PageUtils;
import com.lcredu.common.util.R;
import com.lcredu.common.valid.SaveGroup;
import com.lcredu.common.valid.UpdateGroup;
import com.lcredu.common.valid.UpdateIsShowGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lcredu.lcrliving.commodity.entity.BrandEntity;
import com.lcredu.lcrliving.commodity.service.BrandService;


/**
 * 家居品牌
 *
 * @author lcr
 * @email lcr@gmail.com
 * @date 2023-09-21 16:07:18
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        PageUtils page = brandService.queryPage(params);


        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id) {
        BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     *
     * @Validated 启用jsr303对BrandEntity字段校验
     * 如果没有写就会不生效
     * BindingResult result： springboot会将校验的错误放入到result
     * 我们可以通过BindingResult result将校验的错误进行取出，然后进行相对应的处理
     * 做了异常处理类LcrlivingExceptionControllerAdvice
     * @Validated({SaveGroup.class}) 表示调用save时, 进行参数校验, 使用的是SaveGroup的校验规则, 为什么能用？引入了common模块
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:brand:save")
    public R save(@Validated({SaveGroup.class}) @RequestBody BrandEntity brand/*BindingResult result*/) {

//        //1.创建一个map用来收集错误信息
//        HashMap<String, String> map = new HashMap<>();
//
//        if (result.hasErrors()) {
//            //遍历result,将错误的信息收集到map
//            result.getFieldErrors().forEach((item) -> {
//                //先得到field
//                String field = item.getField();
//                //得到校验错误信息
//                String message = item.getDefaultMessage();
//                //放入map
//                map.put(field, message);
//            });
//            return R.error(400, "品牌表单数据不合法").put("data", map);
//        } else {
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     * @Validated({UpdateGroup.class}): 进行表单修改时的参数校验,使用的是UpdateGroup的校验规则
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/update/isshow")
//    @RequiresPermissions("commodity:brand:update")
    public R updateIsShow(@Validated({UpdateIsShowGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    @RequestMapping("/save2")
//    @RequiresPermissions("commodity:brand:update")
    public R update2(@RequestBody BrandEntity brand) {
        int num = 10 / 0;
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids) {
        brandService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
